package br.com.advance.automacao.services.impl;

import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import br.com.advance.automacao.dao.RegistroErroSistemaDao;
import br.com.advance.automacao.enumeration.TipoErro;
import br.com.advance.automacao.model.RegistroErroSistema;
import br.com.advance.automacao.services.RegistroErroSistemaService;

@Service
@Transactional("transactionManager")
public class RegistroErroSistemaServiceImpl implements RegistroErroSistemaService {

	@SuppressWarnings("unused")
	private static final Logger logger = Logger.getLogger(RegistroErroSistemaServiceImpl.class);

	@Autowired
	private RegistroErroSistemaDao registroErroSistemaDao;

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
	public RegistroErroSistema criar(TipoErro tipo, String descricao) {
		RegistroErroSistema r = new RegistroErroSistema();
		r.setTipoErro(tipo);
		r.setData(new Date());
		r.setDescricao(descricao.length() > 1000 ? descricao.substring(0, 1000) : descricao);
		return registroErroSistemaDao.create(r);
	}

	@Override
	public List<RegistroErroSistema> buscarTodos() {
		return registroErroSistemaDao.findAll();
	}

	@Override
	public void apagar(RegistroErroSistema registroErroSistema) {
		registroErroSistemaDao.delete(registroErroSistema);
	}

}
